<?php

namespace app\manage\middleware;

use Closure;
use app\manage\services\Log;
use think\Request;
use think\Response;

class Logging
{
    protected $logService;

    public function __construct()
    {
        $this->logService = new Log();
    }

    /**
     * 处理请求
     *
     * @param Request $request
     * @param Closure $next
     * @return Response
     */
    public function handle(Request $request, Closure $next): Response
    {
        //记录操作日志
        $server = $request->server();
        if(in_array($request->method(),['POST','PUT','DELETE'])){
            switch ($request->method()){
                case 'PUT':
                    $message = "编辑";
                    break;
                case 'DELETE':
                    $message = "删除";
                    break;
                default:
                    $message = "添加";
                    break;
            }
            $logData = array(
                'uid' => $request->uid,
                'message' => $message,
                'path' => $server['PATH_INFO'],
                'type' => 2,
                'content' => json_encode($request->post())
            );
            $this->logService->add($logData);
        }
        return $next($request);
    }
}
